package com.qf.web;

import com.qf.pojo.*;
import com.qf.service.OrderService;
import com.qf.service.serviceimpl.OrderServiceImpl;
import com.qf.utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
import java.util.Date;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    /**
     * 生成订单
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void commitorder(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){//判断用户是否登录
            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp");
            return;
        }
        /*String methodName = request.getParameter("methodName");
        System.out.println(methodName);*/
        Order order = new Order();//创建订单实体
        System.out.println(user);
        order.setUser(user);//订单属于哪个用户
        order.setOid(UUIDUtils.getId());//封装订单id
        Date date = new Date();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        order.setOrdertime(localDateTime);//封装下单时间
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        order.setTotal(cart.getTotalPrice());//封装总金额
        Collection<CartItem> cartItems = cart.getCartItems();
        OrderItem orderItem = null;
        for(CartItem cartItem : cartItems){
            orderItem = new OrderItem();
            orderItem.setItemid(UUIDUtils.getId());//封装订单项id
            orderItem.setCount(cartItem.getProductCount());//封装订单项商品数量
            orderItem.setSubtatal(cartItem.getSubtotalAmount());//封装订单项小计金额
            orderItem.setProduct(cartItem.getProduct());//封装订单项产品实体
            orderItem.setOrder(order);
            order.getOrderItems().add(orderItem);//
            System.out.println(order);
            //System.out.println(flag);
        }
        OrderService os = new OrderServiceImpl();
        System.out.println(order);
        os.addOrder(order);//调用业务层完成操作
        request.setAttribute("order",order);
        request.getSession().removeAttribute("cart");//将cart从购物车中删除
        request.getRequestDispatcher("/jsp/order_info.jsp").forward(request,response);
    }

    /**
     * 查询订单列表
     * @param request
     * @param response
     */
    public void foundOrderByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
            return;
        }
        //接受前台参数
        String cPage = request.getParameter("currentPage");
        String pSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(cPage);
        int pageSize = Integer.parseInt(pSize);
        //传递数据,调用业务层完成
        OrderService os = new OrderServiceImpl();
        PageBean<Order> listOrder = os.foundOrder(currentPage,pageSize,user.getUid());
        //System.out.println(listOrder);
        request.setAttribute("orders",listOrder);
        request.getRequestDispatcher("/jsp/order_list.jsp").forward(request,response);
    }

    /**
     * 提交订单
     */
    public void pay(){

    }
}
